package com.ftg.learn.chapter08.pattern.myreflech;

import java.beans.IntrospectionException;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * 狮子工厂
 *
 * @author lijiaxin
 * @version 1.0
 * @date 2023/8/7 15:46
 */

public class LionFactory {

    public static Lion getLion(String name, String color) {

        //反射
//        Class cttt = //1 类。class  类名 a = new 类名（）a.getClass()  Class.forName("类地址")
        Class<Lion> l1 = Lion.class;
        Lion mylion = null;
        try {
            mylion = l1.newInstance();
            //Lion mylion = new Lion();

            //l1里面的字段 名称 name     通过属性
//            Field f1 = l1.getDeclaredField("name");
//            f1.set(mylion, name);
//            Field f2 = l1.getDeclaredField("color");
//            f2.set(mylion, color);

            //反射的技术给 name color
//              通过方法
            Method setNameMethod = l1.getDeclaredMethod("setName", String.class);
            setNameMethod.invoke(mylion, name);

//            mylion.setName(name);
            Method setColorMethod = l1.getDeclaredMethod("setColor", String.class);
            setColorMethod.invoke(mylion, color);
        } catch (InstantiationException | IllegalAccessException | NoSuchMethodException | InvocationTargetException e) {
            e.printStackTrace();
        }
        return mylion;
    }

    public static Lion question01(String name, String color) {

        Class c1 = Lion.class;
        Object obj = null;
        try {
            obj = c1.newInstance();
//            字段
            Field[] fields = c1.getDeclaredFields();
            for (Field field : fields) {
                PropertyDescriptor propertyDescriptor = new PropertyDescriptor(field.getName(), c1);
                Method getNameMethod = propertyDescriptor.getReadMethod();
                Method setNameMethod = propertyDescriptor.getWriteMethod();
//                通过setName方法 来name赋值
                setNameMethod.invoke(obj, field.getName().equals(name) ? name : color);
            }

        } catch (InstantiationException | IllegalAccessException | IntrospectionException | InvocationTargetException e) {
            e.printStackTrace();
        }
        return (Lion) obj;
    }

//    工厂就是把创建的细节封装起来 ，不让使用者知道

    public static void main(String[] args) {
//        LionFactory lionFactory=new LionFactory();
//        Lion li= getLion("辛巴","红色");
//        System.out.println(li.getName());

        String[][] lionInfo = {
                {"辛巴", "红色"},
                {"辛巴1", "红色"},
                {"辛巴2", "红色"},
                {"辛巴3", "红色"},
                {"辛巴4", "红色"},
                {"辛巴5", "红色"},
                {"辛巴6", "红色"},
                {"辛巴7", "红色"}
        };
        Lion[] lions = new Lion[lionInfo.length];
        int x = 0;
        for (String[] strings : lionInfo) {
            Lion l = LionFactory.getLion(strings[0], strings[1]);
            lions[x++] = l;
        }
        for (int i = 0; i < lions.length; i++) {
            System.out.println(lions[i].getName()+lions[i].getColor());
        }
    }
}
